package com.ecas.code.generator.util;

import com.ecas.code.generator.entiy.ColumnEntiy;
import com.ecas.code.generator.entiy.TemplateDataEntiy;
import com.ecas.code.generator.enums.ValueTypeEnum;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.util.List;
import java.util.Map;
@Slf4j
public class TemplateDataUtil {
    private static final String TABLE_NAME = "tableName";

    private TemplateDataUtil() {
    }


    public static Map<String,Object> getTemplateDataMap(Map<String,String> tableInfoMap, List<Map<String,String>> columns) {
        Map<String, Object> templateDataMap = Maps.newHashMap();
        TemplateDataEntiy templateDataEntiy = getTemplateDataEntiy( tableInfoMap,  columns);
        templateDataMap.put("moduleName",templateDataEntiy.getTableNamePrx());
        templateDataMap.put("tableComment",templateDataEntiy.getTableComment());
        templateDataMap.put("className",templateDataEntiy.getClassName());
        templateDataMap.put("columns",templateDataEntiy.getColumnEntiyList());
        templateDataMap.put("primeryKey",templateDataEntiy.getTableNamePrx());
        return templateDataMap;
    }

    private static TemplateDataEntiy getTemplateDataEntiy(Map<String,String> tableInfoMap, List<Map<String,String>> columns){
        TemplateDataEntiy templateDataEntiy = new TemplateDataEntiy();
        templateDataEntiy.setTableName(tableInfoMap.get(TABLE_NAME));
        templateDataEntiy.setTableComment(tableInfoMap.get("tableComment"));
        templateDataEntiy.setPrimeryKey(tableInfoMap.get("primeryKey"));
        templateDataEntiy.setTableNamePrx(tableInfoMap.get(TABLE_NAME).split("_")[0]);
        templateDataEntiy.setClassName(StringUtils.capitalize(tableInfoMap.get(TABLE_NAME).split("_")[1]));
        if(columns == null || columns.isEmpty()) {
            log.error("getTemplateData, columns is null or size=0 columns={}",columns);
            return templateDataEntiy;
        }
        List<ColumnEntiy> columnEntiyList = Lists.newArrayList();
        for (Map<String, String> column : columns) {

            ColumnEntiy columnEntiy = new ColumnEntiy();
            columnEntiy.setColumnId(String.valueOf(column.get("columnId")));
            columnEntiy.setColumnKey(column.get("columnKey"));

            String columnName = column.get("columnName");
            columnEntiy.setColumnName(columnName);
            columnEntiy.setPropertyName(CamelCaseUtil.columnNameToPropertyName(columnName));
            columnEntiy.setMethodName(CamelCaseUtil.columnNameToMethodName(columnName));

            String dataType = column.get("dataType");
            columnEntiy.setDataType(dataType);
            columnEntiy.setJavaType(ValueTypeEnum.parseToJavaObjectType(dataType));
            columnEntiy.setPbType(ValueTypeEnum.parseToPbType(dataType));

            columnEntiy.setColumnDefault(column.get("columnDefault"));
            columnEntiy.setColumnComment(column.get("columnComment"));
            columnEntiyList.add(columnEntiy);
        }
        templateDataEntiy.setColumnEntiyList(columnEntiyList);
        return templateDataEntiy;
    }
}
